using System;
using System.Diagnostics;

namespace NBox.Service.Logging
{
    public sealed class LogManager
    {
        private static ILoggerFactory _factory = null;
        private static IConfigurationReader _configurationReader = new ConfigurationReader();
        private static object _loadLock = new object();
        private static readonly string COMMON_SECTION_LOGGING = "nbox.service/logging";

        private LogManager()
        {
        }

        private static ILoggerFactory BuildLoggerFactory()
        {
            ConfigurationSetting section = null;
            try
            {
                section = (ConfigurationSetting)ConfigurationReader.GetSection(COMMON_SECTION_LOGGING);
            }
            catch (Exception exception)
            {
                throw new ConfigurationException("Could not configure NBox.Service.Logging from configuration section 'nbox.service/logging'.", exception);
            }
            if ((section != null) && !typeof(ILoggerFactory).IsAssignableFrom(section.FactoryType))
            {
                throw new ConfigurationException("Specified Factory does not implement ILoggerFactory.  Check implementation of class " + section.FactoryType.AssemblyQualifiedName);
            }
            ILoggerFactory factory = null;
            if (section != null)
            {
                try
                {
                    if (section.Properties.Count > 0)
                    {
                        object[] args = new object[] { section.Properties };
                        factory = (ILoggerFactory)Activator.CreateInstance(section.FactoryType, args);
                    }
                    else
                    {
                        factory = (ILoggerFactory)Activator.CreateInstance(section.FactoryType);
                    }
                }
                catch (Exception exception2)
                {
                    throw new ConfigurationException("Unable to create instance of type " + section.FactoryType.FullName + ". Possible explanation is lack of zero arg and single arg NameValueCollection constructors", exception2);
                }
                return factory;
            }

            ILoggerFactory factory2 = new NBox.Service.Logging.NullLogFactory();
            Trace.WriteLine("Unable to read configuration section nbox.service/logging.  Using no-op implemenation.");
            return factory2;
        }

        public static ILogger GetLogger(string name)
        {
            return Factory.Create(name);
        }

        public static ILogger GetLogger(Type type)
        {
            return Factory.Create(type);
        }

        public static ILoggerFactory Factory
        {
            get
            {
                if (_factory == null)
                {
                    lock (_loadLock)
                    {
                        if (_factory == null)
                        {
                            _factory = BuildLoggerFactory();
                        }
                    }
                }
                return _factory;
            }
            set
            {
                lock (_loadLock)
                {
                    _factory = value;
                }
            }
        }

        public static IConfigurationReader ConfigurationReader
        {
            get
            {
                return _configurationReader;
            }
            set
            {
                _configurationReader = value;
            }
        }
    }
}
